package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyhttp;

import com.test.dababy.dababy_00_utils.DababyConfigs;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class DababyASPostSyncTsk {

    private final String url;
    private final String taskId;
    private final String body;

    public float ldsY=0.52665365f;
    public int dSPF=88;
    public float y844=0.6143115f;
    public float g51o=0.03335762f;
    public boolean gW7N=false;

    public DababyASPostSyncTsk(String taskId, String url, String body) {
        this.taskId = taskId;
        this.url = url;
        this.body = body;
    }

    public DababyASResp getASResp() {
        try {
            URL url = new URL(this.url);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);
            urlConnection.setReadTimeout(180000);
            urlConnection.setConnectTimeout(120000);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Charset", "UTF-8");
            urlConnection.setRequestProperty("Connection", "Keep-Alive");
            urlConnection.setRequestProperty("Content-Type", "application/json");
//            urlConnection.setRequestProperty("Content-Length", (body.getBytes()).length + "");
            urlConnection.setRequestProperty("product", DababyConfigs.dababyProduct);
            urlConnection.getOutputStream().write(body.getBytes());
            urlConnection.getOutputStream().flush();
            urlConnection.getOutputStream().close();
            try {
                int resCode = urlConnection.getResponseCode();
                if (resCode == HttpURLConnection.HTTP_OK) {
                    InputStream in = urlConnection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder result = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }

//                    JSONObject resultObj = new JSONObject(result.toString());
                    return new DababyASResp(this.taskId, new JSONObject());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                urlConnection.getInputStream().close();
                urlConnection.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new DababyASResp(this.taskId, null);
    }

    public String getUrl() {
        return url;
    }

    public String getTaskId() {
        if(dSPF>0 && gW7N){
            for (int i=0;i<dSPF%10;i++){
                y844+=g51o;
            }
            setLdsY(y844);
        }
        return taskId;
    }

    public String getBody() {
        return body;
    }

//         public float ldsY=0.52665365f;
//         public int dSPF=88;
//         public float y844=0.6143115f;
//         public float g51o=0.03335762f;
//         public boolean gW7N=false;

    public void setLdsY(float ldsY) {
        this.ldsY = ldsY;
    }

    public void setdSPF(int dSPF) {
        this.dSPF = dSPF;
    }

    public void setY844(float y844) {
        this.y844 = y844;
    }

    public void setG51o(float g51o) {
        this.g51o = g51o;
    }

    public void setgW7N(boolean gW7N) {
        this.gW7N = gW7N;
    }
}
